package com.java110.api.listener.electric;

import com.alibaba.fastjson.JSONObject;
import com.java110.api.listener.AbstractServiceApiListener;
import com.java110.core.annotation.Java110Listener;
import com.java110.core.context.DataFlowContext;
import com.java110.core.event.service.api.ServiceDataFlowEvent;
import com.java110.dto.machine.MachineDto;
import com.java110.intf.fee.IMeterWaterInnerServiceSMO;
import com.java110.utils.constant.ServiceCodeConstant;
import com.java110.utils.util.BeanConvertUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

/**
 * 新增水电设备
 */
@Java110Listener("weSaveListener")
public class WeSaveListener extends AbstractServiceApiListener {

    @Autowired
    private IMeterWaterInnerServiceSMO meterWaterInnerServiceSMOImpl;

    private static Logger logger = LoggerFactory.getLogger(WeSaveListener.class);
    @Override
    public String getServiceCode() {
        return ServiceCodeConstant.WE_SAVE;
    }

    @Override
    public HttpMethod getHttpMethod() {
        return HttpMethod.POST;
    }

    @Override
    protected void validate(ServiceDataFlowEvent event, JSONObject reqJson) {

    }

    @Override
    protected void doSoService(ServiceDataFlowEvent event, DataFlowContext context, JSONObject reqJson) {
        logger.debug("WeSaveListener : {}", event);
        DataFlowContext dataFlowContext = event.getDataFlowContext();
        MachineDto dto = BeanConvertUtil.covertBean(reqJson, MachineDto.class);

        JSONObject json = new JSONObject();
        int count = meterWaterInnerServiceSMOImpl.countWeByCodeAndType(dto);
        if(count > 0){
            json.put("code",-1);
            json.put("msg","设备编号已存在");
            ResponseEntity<String> responseEntity = new ResponseEntity<String>(json.toJSONString(),HttpStatus.OK);
            dataFlowContext.setResponseEntity(responseEntity);
            return;
        }

        count = meterWaterInnerServiceSMOImpl.countWeByRootAndType(dto);
        if(count > 0){
            json.put("code",-1);
            json.put("msg","该位置已有水电设备");
            ResponseEntity<String> responseEntity = new ResponseEntity<String>(json.toJSONString(),HttpStatus.OK);
            dataFlowContext.setResponseEntity(responseEntity);
            return;
        }

        int savewe = meterWaterInnerServiceSMOImpl.savewe(dto);
        if(savewe == 1){
            json.put("code",0);
            json.put("msg","ok");
            ResponseEntity<String> responseEntity = new ResponseEntity<String>(json.toJSONString(),HttpStatus.OK);
            dataFlowContext.setResponseEntity(responseEntity);
        } else {
            json.put("code",-1);
            json.put("msg","system error");
            ResponseEntity<String> responseEntity = new ResponseEntity<String>(json.toJSONString(),HttpStatus.OK);
            dataFlowContext.setResponseEntity(responseEntity);
        }
    }


    @Override
    public int getOrder() {
        return 0;
    }
}
